package br.com.sgc.entities.seguranca;

import java.io.Serializable;
import java.util.Arrays;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import br.com.sgc.entities.geral.Clinica;

@Entity
@Table(name="perfil", schema="seguranca")
public class Perfil implements Serializable{

	private static final long serialVersionUID = 6354872118499698278L;
	
	@Id
	@SequenceGenerator(sequenceName = "seguranca.seq_perfil", name = "SEQUSER", allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUSER")
	private Long id;
	
	@Column(name="nome_perfil")
	private String nomePerfil;
	
	@Column(columnDefinition = "text")
	private String permissoes;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="clinica_id")
	private Clinica clinica = new Clinica();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public String getNomePerfil() {
		return nomePerfil;
	}

	public void setNomePerfil(String nomePerfil) {
		this.nomePerfil = nomePerfil;
	}

	public String getPermissoes() {
		return permissoes;
	}

	public String[] getPermissoesArray() {
		return permissoes == null ? null : permissoes.split(",");
	}

	public boolean hasPermissao(String role) {
		String[] p = getPermissoesArray();
		//TODO: Investigar forma mais barata de executar o código abaixo
		return p == null ? false : Arrays.asList(p).contains(role);
	}

	public void setPermissoes(String permissoes) {
		this.permissoes = permissoes.replace("[", "").replace("]", "")
				.replace(" ", "");
	}

	public Clinica getClinica() {
		return clinica;
	}

	public void setClinica(Clinica clinica) {
		this.clinica = clinica;
	}
}
